/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.pojo.User;
import org.springframework.core.SpringVersion;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>Project: springmvc2021 - IndexController
 * <p>Powered by webrx On 2021-11-04 16:25:41
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

@Controller
public class IndexController {
    @RequestMapping("/")
    public void index(HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.printf("<h2>Hello world 中文效果,SpringMVC [%s]</h2>", SpringVersion.getVersion());
        out.flush();
        out.close();
    }

    @GetMapping("/index")
    @ResponseBody
    public String index() {
        return String.format("<h2>Hello world 中文效果,SpringMVC [%s]</h2>", SpringVersion.getVersion());
    }

    @GetMapping("/show")
    public void show(String name, HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"utf-8\">");
        out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
        out.println("<meta name=\"keywords\" content=\"关键字\">");
        out.println("<meta name=\"description\" content=\"简介\">");
        out.println("</head>");
        out.println("<body>");

        out.println("<h3>Hello World  中文效果" + name + "</h3>");

        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }

    @PostMapping("/hello")
    @ResponseBody
    public String hello(String name, int id) {
        System.out.println(name);
        System.out.println(id);
        return name + id;
    }

    @RequestMapping("/abc")
    public void abc(PrintWriter out) {

        out.print("中文字符串ass");
    }

    @GetMapping("/uus")
    @ResponseBody
    public List<User> uus() {
        List<User> list = new ArrayList<User>();
        var u = new User();
        u.setId(10);
        u.setName("李四");
        u.setRegtime(new Date());
        u.setBirth(LocalDate.of(1988, 5, 10));
        u.setCtime(LocalDateTime.now());
        list.add(u);
        list.add(u);
        list.add(u);
        return list;
    }

    @GetMapping("/uu")
    @ResponseBody
    public User uu() {
        var u = new User();
        u.setId(10);
        u.setName("李四");
        u.setRegtime(new Date());
        u.setBirth(LocalDate.of(1988, 5, 10));
        u.setCtime(LocalDateTime.now());
        return u;
    }


    @GetMapping("/uud")
    @ResponseBody
    public List<LocalDate> uud() {
        List<LocalDate> list = new ArrayList<LocalDate>();
        list.add(LocalDate.now());
        list.add(LocalDate.now());
        list.add(LocalDate.of(2003, 5, 6));
        list.add(LocalDate.now());
        return list;
    }

    @GetMapping("/uudd")
    @ResponseBody
    public LocalDateTime uudd() {
        return LocalDateTime.now();
    }

    @GetMapping("/uuda")
    @ResponseBody
    public Date uuda() {
        return new Date();
    }


    @GetMapping("/myuser")
    public String user(Model m) {
        m.addAttribute("name", "李四");
        return "user";
    }
}
